﻿using System;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace P2P_Backup.forms.controls
{
    public class LocalFileConnectionListView : ListView
    {
        public LocalFileConnectionListView()
        {
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.EnableNotifyMessage, true);


            this.View = System.Windows.Forms.View.Details;
            this.GridLines = true;
            this.Columns.Add("Filename", 200);
            this.Columns.Add("Status",70);
            this.Columns.Add("Peer", 120);
            this.Columns.Add("Size");
            this.Columns.Add("Progress");
            this.Columns.Add("Speed", 100);
                        
        }
        protected override void OnNotifyMessage(Message m)
        {         
            if (m.Msg != 0x14)
            {
                base.OnNotifyMessage(m);
            }
        }
        public void setConnections(List<connections.LocalFileConnection> connections)
        {            
            connections.ItemAdded += connections_ItemAdded;
            connections.ItemRemoved += connections_ItemRemoved;
        }

        private void connections_ItemAdded(object sender, ItemArgs<connections.LocalFileConnection> e)
        {
            if (this.InvokeRequired) this.BeginInvoke((MethodInvoker)delegate { connections_ItemAdded(sender, e); });
            else
            {
                ListViewItem item = new ListViewItem(e.Item.file.Name());
                item.SubItems.Add(new ListViewItem.ListViewSubItem(item, e.Item.file.Status.ToString()));
                item.SubItems.Add(new ListViewItem.ListViewSubItem(item, e.Item.file.BackupPeer.ToString()));
                item.SubItems.Add(new ListViewItem.ListViewSubItem(item, e.Item.file.FriendlySize()));
                item.SubItems.Add(new ListViewItem.ListViewSubItem(item, "0"));
                item.SubItems.Add(new ListViewItem.ListViewSubItem(item, "0"));

                Items.Add(item);

                e.Item.Progress.Progressed += FileConnection_Progess;
            }
        }

        private void connections_ItemRemoved(object sender, ItemArgs<connections.LocalFileConnection> e)
        {
            if (this.InvokeRequired) this.BeginInvoke((MethodInvoker)delegate { connections_ItemRemoved(sender, e); });
            else
            {
                ListViewItem item = this.FindItemWithText(e.Item.file.Name(), false, 0);
                Items.Remove(item);
                e.Item.Progress.Progressed -= FileConnection_Progess;
            }
        }

        private void FileConnection_Progess(object sender, connections.FileConnectionArgs e)
        {
            if (this.InvokeRequired) this.BeginInvoke((MethodInvoker)delegate { FileConnection_Progess(sender, e); });
            else
            {
                ListViewItem item = this.FindItemWithText(e.File.Name(), false, 0);
                if (item != null)
                {
                    item.SubItems[1].Text = e.File.Status.ToString();
                    item.SubItems[4].Text = String.Format("{0} %", e.Progress);
                    item.SubItems[5].Text = String.Format("{0} mb/s",e.Speed);                    
                }
            }
        }
    }
}
